package com.avasthi.cli.twitter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;

import de.l3s.boilerpipe.BoilerpipeProcessingException;
import de.l3s.boilerpipe.extractors.ArticleExtractor;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

public class TwitterJsonParser {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		if (args.length == 0) {
			System.out.println("Usage: java TwitterJsonParser twitterlog");
			System.exit(0);
		}
		File inp = new File(args[0]);
		System.out.println("Reading file :" + inp.getAbsolutePath());
		try {
			FileReader fr = new FileReader(inp);
			BufferedReader reader = new BufferedReader(fr);
			JSONParser parser = new JSONParser(JSONParser.MODE_PERMISSIVE);
			JSONObject jv = null;
			while ((jv = (JSONObject) parser.parse(reader)) != null) {

				Long id = (Long) jv.get("id");
				JSONObject retweeted_status = (JSONObject) jv
						.get("retweeted_status");
				JSONObject entities = (JSONObject) jv.get("entities");
				if (retweeted_status != null) {

					System.out.println(id + " "
							+ retweeted_status.toJSONString());
					PrintWriter writer = new PrintWriter(
							"/home/vavasthi/tweets/" + id, "UTF-8");
					writer.println((String) retweeted_status.get("text"));
					if (entities != null) {

						{

							JSONArray urls = (JSONArray) entities.get("urls");
							if (urls != null) {
								System.out.println("Found URLs" + urls.size());
								for (JSONObject u : urls
										.toArray(new JSONObject[urls.size()])) {
									String su = u.get("expanded_url")
											.toString();
									try {
										URL url = new URL(su);
										String text = ArticleExtractor.INSTANCE
												.getText(url);
										writer.println(text);
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (BoilerpipeProcessingException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (Exception e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
						}
						{

							JSONArray hashtags = (JSONArray) entities
									.get("hashtags");
							if (hashtags != null) {
								System.out.println("Found Hashtags"
										+ hashtags.size());
								for (JSONObject h : hashtags
										.toArray(new JSONObject[hashtags.size()])) {
									String ht = h.get("text").toString();
									writer.println(ht);
								}
							}
						}
					}
					writer.close();
				} else {

					System.out.println(id);
				}
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
